
var express = require('express');
var router = express.Router();
var sqlQuery = require('../../../module/sql');
var jiami = require('../../../module/jiami');

router.get('/',async(req,res)=>{
    res.render('admin/user/list')

})



router.get('/api',async(req,res)=>{
  let page = parseInt(req.query.page);  //第几页
  let limitNum = parseInt(req.query.limit); //限制多少条
  let sqlStr = 'select `user`.id,`user`.username,`user`.email,`user`.phone,`user`.state,`user`.roleid,role.rolename,role.brief from user LEFT JOIN role on `user`.roleid=role.id limit ?,?'
  let arr = [(page-1)*limitNum,limitNum]
  let result = await sqlQuery(sqlStr,arr);
  //获取用户的总数
  let sqlStr1 = "select count(id) as number from user"
  let result1 = await sqlQuery(sqlStr1)
  let count = result1[0].number
  let options ={
    "code": 0,
    "msg": "",
    "count": count,
    "data": Array.from(result)
  }
  res.json(options)
})


router.post('/state',async(req,res)=>{
  console.log(req.body)
  let uid = req.body.id
  let state = req.body.state
  let sqlStr1 = "update user set state=? where id=?";
  let result1 = await sqlQuery(sqlStr1,[state,uid])
  res.json({
    state:"ok",
    content:Array.from(result1)
  })
})


module.exports = router;